package com.free.solon.common.model;

import com.mybatisflex.core.paginate.Page;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;

/**
 * 分页结果返回对象
 *
 * @Author: hao.ding@insentek.com
 * @Date: 2024/5/21 15:30
 */
@Data
public class PageResult<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    public static int SUCCEED_CODE = 200;

    /**
     * 总记录数
     */
    private Long total;

    /**
     * 列表数据
     */
    private List<T> rows;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 消息内容
     */
    private String message;

    public PageResult() {
    }

    /**
     * 分页
     *
     * @param total 总记录数
     * @param list  列表数据
     */
    public PageResult(Long total, List<T> list) {
        this.code = SUCCEED_CODE;
        this.message = "";
        this.rows = list;
        this.total = total;
    }

    public static <T> PageResult<T> build() {
        PageResult<T> rspData = new PageResult<>();
        rspData.setCode(SUCCEED_CODE);
        rspData.setMessage("");
        return rspData;
    }

    public static <T> PageResult<T> build(Page<T> page) {
        PageResult<T> rspData = new PageResult<>();
        rspData.setCode(SUCCEED_CODE);
        rspData.setMessage("");
        rspData.setRows(page.getRecords());
        rspData.setTotal(page.getTotalRow());
        return rspData;
    }
}
